//    Copyright (C) 2013  <lugocreation@gmail.com>
//
//    This program is free software: you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    (at your option) any later version.
//
//    This program is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//    along with this program.  If not, see <http://www.gnu.org/licenses/>.
package org.jpgolay.me.supplier;

import java.math.BigDecimal;
import net.sf.jasperreports.engine.*;
import org.jpgolay.me.util.MEDate;
import org.jpgolay.me.util.Parameter;

// Referenced classes of package org.jpgolay.me.supplier:
//            Supplier
public class SupplierPrintDataSource
        implements JRDataSource {

    public SupplierPrintDataSource(Supplier supplier, Object piecesBuy[][], Parameter param) {
        this.supplier = supplier;
        this.piecesBuy = piecesBuy;
        this.param = param;
        date = new MEDate();
        totalDue = new BigDecimal("0.00");
        totalNetPrice = new BigDecimal("0.00");
        countPiecesListLine = 0;
        count = 35 * piecesBuy.length;
    }

    public boolean next()
            throws JRException {
        return count > 0;
    }

    public Object getFieldValue(JRField field)
            throws JRException {
        String fieldname = field.getName();
        String value = null;
        if ("id".equals(fieldname)) {
            if (supplier.getId() != null) {
                value = "Code : " + supplier.getId().toString();
            }
        } else if ("company".equals(fieldname)) {
            value = supplier.getRaisonSociale();
        } else if ("status".equals(fieldname)) {
            value = supplier.getCivilite();
        } else if ("sales_addrLn1".equals(fieldname)) {
            value = supplier.getAdresseComUn();
        } else if ("sales_addrLn2".equals(fieldname)) {
            value = supplier.getAdresseComDeux();
        } else if ("sales_zip".equals(fieldname)) {
            value = supplier.getCpCom();
        } else if ("sales_city".equals(fieldname)) {
            value = supplier.getVilleCom();
        } else if ("sales_country".equals(fieldname)) {
            value = supplier.getPaysCom();
        } else if ("shipping_addrLn1".equals(fieldname)) {
            value = supplier.getAdresseEnlevUn();
        } else if ("shipping_addrLn2".equals(fieldname)) {
            value = supplier.getAdresseEnlevDeux();
        } else if ("shipping_zip".equals(fieldname)) {
            value = supplier.getCpEnlev();
        } else if ("shipping_city".equals(fieldname)) {
            value = supplier.getVilleEnlev();
        } else if ("shipping_country".equals(fieldname)) {
            value = supplier.getPaysEnlev();
        } else if ("tel1".equals(fieldname)) {
            if (supplier.getTelephone() != null) {
                value = "T\351l 1 : " + supplier.getTelephone();
            }
        } else if ("fax".equals(fieldname)) {
            if (supplier.getFax() != null) {
                value = "Fax : " + supplier.getFax();
            }
        } else if ("tel2".equals(fieldname)) {
            if (supplier.getPortable() != null) {
                value = "T\351l 2 : " + supplier.getPortable();
            }
        } else if ("email".equals(fieldname)) {
            if (supplier.getEmail() != null) {
                value = "Email : " + supplier.getEmail();
            }
        } else if ("web".equals(fieldname)) {
            if (supplier.getSiteWeb() != null) {
                value = "Site web : " + supplier.getSiteWeb();
            }
        } else if ("gln".equals(fieldname)) {
            if (supplier.getGln() != null) {
                value = "GLN : " + supplier.getGln();
            }
        } else if ("last_modify".equals(fieldname)) {
            String date = supplier.getUpdateLastTime().substring(6, 8) + "/" + supplier.getUpdateLastTime().substring(4, 6) + "/" + supplier.getUpdateLastTime().substring(0, 4) + " " + supplier.getUpdateLastTime().substring(8, 10) + ":" + supplier.getUpdateLastTime().substring(10, 12) + ":" + supplier.getUpdateLastTime().substring(12);
            value = date;
        } else if ("contact".equals(fieldname)) {
            if (supplier.getNomPrenomContact() != null) {
                value = "Contact : " + supplier.getNomPrenomContact();
            }
        } else if ("job_contact".equals(fieldname)) {
            if (supplier.getFonctionContact() != null) {
                value = "Fonction : " + supplier.getFonctionContact();
            }
        } else if ("bank_number".equals(fieldname)) {
            if (supplier.getNumeroCompte() != null) {
                value = "N\260 compte : " + supplier.getNumeroCompte();
            }
        } else if ("piece_id_txt".equals(fieldname)) {
            value = "N\260";
        } else if ("piece_id".equals(fieldname)) {
            value = piecesList(0);
        } else if ("date_txt".equals(fieldname)) {
            value = "Date";
        } else if ("date".equals(fieldname)) {
            value = piecesList(1);
        } else if ("delivery_date_txt".equals(fieldname)) {
            value = "Re\347u le";
        } else if ("delivery_date".equals(fieldname)) {
            value = piecesList(2);
        } else if ("due_txt".equals(fieldname)) {
            value = "Reste du";
        } else if ("due".equals(fieldname)) {
            value = piecesList(3);
        } else if ("total_txt".equals(fieldname)) {
            value = "TTC";
        } else if ("total".equals(fieldname)) {
            value = piecesList(4);
        } else if ("total_due".equals(fieldname)) {
            value = totalDue.setScale(param.getMinimumDecimalNumber().intValue(), param.getRoundType().intValue()).toString();
        } else if ("total_net_price".equals(fieldname)) {
            value = totalNetPrice.setScale(param.getMinimumDecimalNumber().intValue(), param.getRoundType().intValue()).toString();
        }
        count--;
        return value;
    }

    private String piecesList(int col) {
        if (countPiecesListLine < piecesBuy.length) {
            switch (col) {
                case 0: // '\0'
                    return (String) piecesBuy[countPiecesListLine][col];

                case 1: // '\001'
                    return date.toME((String) piecesBuy[countPiecesListLine][col]);

                case 2: // '\002'
                    return date.toME((String) piecesBuy[countPiecesListLine][col]);

                case 3: // '\003'
                    if (piecesBuy[countPiecesListLine][col] != null) {
                        totalDue = totalDue.add(new BigDecimal((String) piecesBuy[countPiecesListLine][col]));
                        return (new BigDecimal((String) piecesBuy[countPiecesListLine][col])).setScale(param.getMinimumDecimalNumber().intValue(), param.getRoundType().intValue()).toString();
                    } else {
                        return null;
                    }

                case 4: // '\004'
                    if (piecesBuy[countPiecesListLine][col] != null) {
                        totalNetPrice = totalNetPrice.add(new BigDecimal((String) piecesBuy[countPiecesListLine][col]));
                        return (new BigDecimal((String) piecesBuy[countPiecesListLine++][col])).setScale(param.getMinimumDecimalNumber().intValue(), param.getRoundType().intValue()).toString();
                    } else {
                        return null;
                    }
            }
            return null;
        } else {
            return null;
        }
    }
    private Supplier supplier;
    private Object piecesBuy[][];
    private Parameter param;
    private MEDate date;
    private BigDecimal totalDue;
    private BigDecimal totalNetPrice;
    private int count;
    private int countPiecesListLine;
}
